import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Store {

	public static int shopWidth = 8;
	public static int buttonSize = 52;
	public static int cellSpace = 2;
	public static int awayFromGame = 29;
	public static int iconSize = 20;
	public static int iconSpace = 3;
	public static int iconTextY = 15;

	public static int[] buttonID = { Value.airTowerLaser, Value.airAir,
			Value.airAir, Value.airAir, Value.airAir, Value.airAir,
			Value.airAir, Value.airTrashCan };
	public static int itemIn = 4;
	public static int heldID = -1;
	public Rectangle[] button = new Rectangle[shopWidth];
	public Rectangle buttonHealth;
	public Rectangle buttonCoins;
	public static int[] buttonPrices = {10,0,0,0,0,0,0,0};

	public boolean holdsItem = false;

	public static int realID = -1;
	public Store() {
		define();
	}

	public void define() {
		for (int i = 0; i < button.length; i++) {
			button[i] = new Rectangle((Screen.myWidth / 2)
					- ((shopWidth * (buttonSize + cellSpace)) / 2)
					+ ((buttonSize + cellSpace) * i),
					Screen.room.block[Screen.room.worldHeight - 1][0].y
							+ Screen.room.blockSize + awayFromGame, buttonSize,
					buttonSize);
		}

		buttonHealth = new Rectangle(Screen.room.block[0][0].x - 1,
				button[0].y, iconSize, iconSize);
		buttonCoins = new Rectangle(Screen.room.block[0][0].x - 1, button[0].y
				+ button[0].height - iconSize, iconSize, iconSize);
	}

	public void click(int mouseButton) {
		if (mouseButton == 1) {
			for (int i = 0; i < button.length; i++) {
				if (button[i].contains(Screen.mse)) {
					if (buttonID[i] != Value.airAir) {
						if (buttonID[i] == Value.airTrashCan) {
							holdsItem = false;
							// heldID = Value.airAir;
						} else {
							heldID = buttonID[i];
							holdsItem = true;
							realID = i;
							System.out.println("tutqj");
						}
					}
				}
			}
			if (holdsItem) {
				System.out.println("holditems true");
				if(Screen.coinage >= buttonPrices[realID]) {
					System.out.println("stac mnie");
					for(int y = 0; y < Screen.room.block.length; y++) {
						for(int x =0; x < Screen.room.block[0].length;x++) {
							if (Screen.room.block[y][x].contains(Screen.mse)) {
								System.out.println("jestem w bloku o paramsach: " + y + " oraz " + x); 
								if (Screen.room.block[y][x].groundID !=  Value.groundRoad && Screen.room.block[y][x].airID ==  Value.airAir) {
									Screen.room.block[y][x].airID = heldID;
									Screen.coinage -= buttonPrices[realID];
									System.out.println("stawiam na trawie");
								}
							}
						}
					}
				}
			}
		}
	}

	public void draw(Graphics g) {
		for (int i = 0; i < button.length; i++) {
			if (button[i].contains(Screen.mse)) {
				g.setColor(new Color(255, 255, 255, 150));
				g.fillRect(button[i].x, button[i].y, button[i].width,
						button[i].height);
			}
			// g.fillRect(button[i].x, button[i].y, button[i].width,
			// button[i].height);
			g.drawImage(Screen.tileset_res[0], button[i].x, button[i].y,
					button[i].width, button[i].height, null);
			if (buttonID[i] != Value.airAir)
				g.drawImage(Screen.tileset_air[buttonID[i]], button[i].x
						+ itemIn, button[i].y + itemIn, button[i].width
						- (itemIn * 2), button[i].height - (itemIn * 2), null);
			if (buttonPrices[i] > 0 ) {
			g.setColor(new Color(255,255,255));
			g.setFont(new Font("Couruer new", Font.BOLD, 14));
			g.drawString("$ " + buttonPrices[i], button[i].x
					+ itemIn, button[i].y + itemIn + 10);
			}
		}

		g.drawImage(Screen.tileset_res[1], buttonHealth.x, buttonHealth.y,
				buttonHealth.width, buttonHealth.height, null);
		g.drawImage(Screen.tileset_res[2], buttonCoins.x, buttonCoins.y,
				buttonCoins.width, buttonCoins.height, null);
		g.setFont(new Font("Courier new", Font.BOLD, 14));
		g.setColor(new Color(255, 255, 255));
		g.drawString("" + Screen.health, buttonHealth.x + buttonHealth.width
				+ iconSpace, buttonHealth.y + iconTextY);
		g.drawString("" + Screen.coinage, buttonCoins.x + buttonCoins.width
				+ iconSpace, buttonCoins.y + iconTextY);

		if (holdsItem) {
			g.drawImage(Screen.tileset_air[heldID],
					Screen.mse.x - ((button[0].width - (itemIn * 2)) / 2) + itemIn,
					Screen.mse.y - ((button[0].width - (itemIn * 2)) / 2) + itemIn, 
					button[0].width - (itemIn * 2),
					button[0].height - (itemIn * 2),
				null);
		}
	}
}
